package org.karelun.gef.policies;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gef.requests.CreateRequest;
import org.karelun.gef.commands.ChangeNodeConstraintCommand;
import org.karelun.gef.commands.CreateNodeCommand;
import org.karelun.gef.model.DiagramModel;
import org.karelun.gef.model.Node;
import org.karelun.gef.parts.NodePart;

/**
 * @author Karelun huang
 *
 */
public class DiagramLayoutEditPolicy extends XYLayoutEditPolicy {

    protected Command createAddCommand(EditPart child, Object constraint) {
        return null;
    }

    protected Command createChangeConstraintCommand(EditPart child, Object constraint) {
        if (!(child instanceof NodePart))
            return null;
        if (!(constraint instanceof Rectangle))
            return null;

        ChangeNodeConstraintCommand cmd = new ChangeNodeConstraintCommand();
        cmd.setNode((Node) child.getModel());
        cmd.setLocation(((Rectangle) constraint).getLocation());
        cmd.setDimension(((Rectangle) constraint).getSize());
        return cmd;
    }

    protected Command getCreateCommand(CreateRequest request) {
        Object newObject = request.getNewObject();
		if (!(newObject instanceof Node))
            return null;
        CreateNodeCommand cmd = new CreateNodeCommand();
        Object model = getHost().getModel();
		cmd.setDiagram((DiagramModel) model);
        cmd.setModel( newObject);
        Rectangle constraint = (Rectangle) getConstraintFor(request);
        cmd.setLocation(constraint.getLocation());
        return cmd;
    }

    protected Command getDeleteDependantCommand(Request request) {
        return null;
    }
}